/**
 * 公共service基类
 */

import AnswerStateChecker from './AnswerStateChecker'
import QtiAdapter from './QtiAdapter'
import {
    CompleteState,
    AnswerState,
    Cardinality,
    BaseType
} from './ServiceConst'

import ServiceUtils from './ServiceUtils'
import Event from './Event'


var notIplementException = function () {
    throw new Error('该方法还未实现')
}

class BaseService {
    constructor(id) {
        this.questionId = id;
        this.answerState = null;
        this.userAnswer = null;
        this.event = new Event();
    }


    /**
     * 获取标准的题型model
     * @memberof BaseService
     */
    getQuestionModel() {
        notIplementException()
    }


    /**
     * 获取用户作答状态
     * 
     * @memberof BaseService
     */
    getAnswerState() {
        this.event.trigger('beforeGetAnswerState');
        var answerState = this.answerState;
        if (!answerState) {
            return AnswerStateChecker.getDefaultState(this.getQuestionModel())
        }
        return ServiceUtils.deepClone(answerState);
    }

    /**
     * 
     * 获取用户答案
     * 
     * @memberof BaseService
     */
    getAnswer() {
        return ServiceUtils.deepClone(this.userAnswer);
    }


    /**
     * 状态变更通知
     * 
     * @param {any} name 事件名  answerChange(用户作答变更)
     * @param {any} data 事件数据 
     * @memberof BaseService
     */
    trigger(name, data) {
        let that = this;
        name = name.toLowerCase();
        if (name === 'answerchange') {
            this.userAnswer = data;
            this.answerState = AnswerStateChecker.getCheckState(this.getQuestionModel(), data);
        }
        this.event.trigger(name, data);
    }

    on(name, callback) {
        this.event.on(name.toLowerCase(), callback);
    }

    off(name, callback) {
        this.event.off(name.toLowerCase(), callback);
    }

    /**
     * 返回当前题目提示类型
     * 
     * @memberof BaseService
     * @return  //SIMPLE,MULTBLANK,MULTQUESTION,PARTICAL,提示类型对应题型A,B,C,D
     */
    getTipType() {
        notIplementException();
    }



    /**
     * 用于子类多用户作答结果模型的扩展
     * @param {any} result 作答模型
     * @memberof BaseService
     */
    extendResult(result) {
        notIplementException()
    }


    /**
     * 
     * 获取信息基础信息 
     * @memberof BaseService
     */
    getQuestionInfo() {
        notIplementException()
    }

    /**
     * 显示用户、或正确答案
     * 
     * @param {any} type  ‘user/correct’ 
     * @param {any} specialAnswer 用户答案，可不传
     * @memberof BaseService
     */
    showAnswer(type, specialAnswer) {
        notIplementException()
    }

    /**
     * 锁定题目，不可作答
     * @memberof BaseService
     */
    lockQuestion() {
        notIplementException()
    }

    /**
     * 解锁题目
     * @memberof BaseService
     */
    unlockQuestion() {
        notIplementException()
    }

    /**
     * 结束题目
     * @memberof BaseService
     */
    finishQuestion() {
        notIplementException()
    }

    /**
     * 重置题目
     * @memberof BaseService
     */
    resetQuestion() {
        notIplementException()
    }

    /**
     * 显示用户作答反馈提示
     * @memberof BaseService
     */
    showResultHint() {
        notIplementException()
    }

    static parseQtiModel(qtiModel) {
        return QtiAdapter.parseModel(qtiModel);
    }

}

export {
    BaseService
}